//
//  LongPressButtonView.swift
//  CatPomodoroTimer
//
//  Created by Xuri Li on 2024/8/2.
//

import SwiftUI

struct LongPressButtonView: View {
    var pTimerStatus: PTimerStatus
    @State private var isLongPressing = false
    @State private var progress: CGFloat = 0.0
    @State private var timer: Timer?
    @State private var longPressCompleted = false // 跟踪长按是否完成
    @State private var longPressStartTime: Date?
    @State private var longPressEndTimer: Timer?
    var onStop: (() -> Void)? // 回调函数
    
    var body: some View {
        ZStack {
            Circle()
                .stroke(Color.white, lineWidth: 1)
                .frame(width: 40, height: 40)
                .overlay {
                    Circle()
                        .trim(from: 0, to: progress)
                        .stroke(Color.white, lineWidth: 4)
                        .frame(width: 40, height: 40)
                        .rotationEffect(.degrees(-90))
                        .animation(.linear(duration: 2), value: progress) // 2秒钟的时间
                }
                .overlay {
                    Image(systemName: "stop.fill")
                        .foregroundStyle(.white)
                        .font(.title2)
                }
                .padding(EdgeInsets())
                .gesture(
                    LongPressGesture(minimumDuration: 2)
                        .onChanged { _ in
                            if !isLongPressing {
                                longPressStartTime = Date()
                                startProgress()
                            }
                        }
                        .onEnded({ success in
                            longPressCompleted = success
                            if longPressCompleted {
                                // 开始倒计时 0.5 秒
                                longPressEndTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { _ in
                                    stopAction()
                                }
                            } else {
                                resetProgress()
                            }
                        })
                )
                .simultaneousGesture(
                    DragGesture(minimumDistance: 0)
                        .onEnded({ _ in
                            if longPressCompleted {
                                // 取消结束倒计时
                                longPressEndTimer?.invalidate()
                                stopAction()
                            } else {
                                resetProgress()
                            }
                        })
                )
        }
    }
    
    private func startProgress() {
        guard pTimerStatus != .stop else { return }
        isLongPressing = true
        progress = 0
        timer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true, block: { _ in
            withAnimation {
                if progress < 1 {
                    progress += 0.01 / 1.8 // 2秒钟的时间
                }
            }
        })
    }
    
    private func resetProgress() {
        guard pTimerStatus != .stop else { return }
        isLongPressing = false
        progress = 0
        timer?.invalidate()
        timer = nil
    }
    
    private func stopAction() {
        guard pTimerStatus != .stop else { return }
        resetProgress()
        if longPressCompleted {
            onStop?() // 调用回调函数
            longPressCompleted = false // 重置长按完成状态
        }
    }
}

#Preview {
    LongPressButtonView(pTimerStatus: .stop, onStop: {
        print("Long press completed, timer stopped.")
    })
        .background(.black)
}
